import java.io.*;
import java.util.Hashtable;
import javax.microedition.io.*;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

public final class aq
        implements i, Runnable {

    private static aq j = null;
    private MIDlet k;
    private Display l;
    private bh m;
    private bt n;
    public bl a;
    public ay b;
    private bf o;
    public q c;
    private String p;
    public String d;
    public String e;
    public boolean f;
    private int q;
    private Hashtable r;
    public int g;
    public long h;
    public boolean i;

    public static aq a() {
        return j;
    }

    public aq() {
        e = "";
        f = false;
        r = new Hashtable();
        i = false;
        j = this;
    }

    public final void a(MIDlet midlet) {
        k = midlet;
        a = new bl("client");
        b = new ay(this);
        l = Display.getDisplay(midlet);
        ce.a(l);
        af.a();
        bm.a();
        m = new bh(l, "/default.theme");
        bb.a();
        d.a = new d();
        long l1 = 0L;
        String s;
        if ((s = a("promowidget", ((String) (null)))) != null) {
            int i1 = 0;
            int j1 = s.length();
            do {
                if (i1 >= j1)
                    break;
                char c1;
                if (Character.isDigit(c1 = s.charAt(i1))) {
                    l1 = Character.digit(c1, 10);
                    char c2;
                    for (int k1 = i1 + 1; k1 < j1 && Character.isDigit(c2 = s.charAt(k1)); k1++)
                        l1 = l1 * 10L + (long) Character.digit(c2, 10);

                    break;
                }
                i1++;
            } while (true);
        }
        h = l1;
    }

    private static void a(int i1) {
        try {
            Thread.sleep(i1);
        }
        catch (InterruptedException _ex) {
        }
    }

    public final void b() {
        ce.b("startApp");
        switch (q) {
            case 0:
                run();
                break;
            case 1:
                m.o();
                d.a.b(true);
                break;
            case 2:
                m.o();
                d.a.b(false);
                break;
        }
    }

    public final void c() {
        ce.b("pauseApp");
        q = d.a.b ? 1 : 2;
        d.a.b(false);
        a(333);
    }

    public final void d() throws ConnectionNotFoundException {
        ce.b("destroyApp");
        g();
    }

    public final void a(String s) {
        if ("background".equals(s)) {
            ao.a();
            return;
        }
        if ("proxyTest".equals(s)) {
            (new n(this)).start();
            return;
        }
        p = s;
        if (q != 3) {
            q = 3;
            (new Thread(this)).start();
        }
    }

    private void g() throws ConnectionNotFoundException {
        if (n == null)
            return;
        bm.a();
        aw aw1;
        if ((aw1 = n.a) != null)
            aw1.m();
        a(500);
        m.h();
        d.a.d();
        a(500);
        if (p != null && p.length() > 0)
            c(p);
        k.notifyDestroyed();
    }

    private void h() {
        ce.a("doStart called, UI visible=" + ao.a().b());
        ce.a("Memory usage=" + ce.a());
        ao.a();
        if (!ao.b()) {
            bm.a();
            return;
        }
        q = 1;
        j();
        n = new bt(bh.c());
        a(500);
        m.i();
        bh.j();
        n.b(0, 0, bh.d.f, bh.d.g);
        ce.a("Screen: " + bh.d);
        n.a.b(true);
        ao.a();
        if (ao.b()) {
            l.setCurrent(m);
            m.g();
        }
    }

    private void i() {
        if (ce.a(29)) {
            String s;
            if ((s = b("aurl")) == null || s.length() == 0) {
                s = "http://get.widsets.com/";
                ce.b("Aurl not found, using default.");
            }
            StringBuffer stringbuffer;
            (stringbuffer = new StringBuffer()).append(s);
            stringbuffer.append("ping");
            if (e(stringbuffer.toString()))
                d.a(true);
        }
    }

    private static boolean e(String s) {
        HttpConnection httpconnection = null;

        try {
            httpconnection = (HttpConnection) Connector.open(s);
            (httpconnection).setRequestMethod("GET");
            String s1;
            if ((s1 = httpconnection.getRequestProperty("User-Agent")) == null || s1.startsWith("Mozilla"))
                httpconnection.setRequestProperty("User-Agent", "NokiaN73-1/3.0638/Mozilla/5.0");
            httpconnection.getResponseCode();
            ce.c("No proxy");
            return false;
        } catch (IOException e1) {
            e1.printStackTrace();
            boolean flag1;
            ce.c("Using proxy");
            flag1 = true;
            try {
                if (httpconnection != null) {
                    httpconnection.close();
                }
            }
            catch (Exception _ex) {
                ce.c("Http error " + _ex);
                flag1 = false;
                try {
                    if (httpconnection != null) {
                        httpconnection.close();
                    }
                }
                catch (Exception _ex1) {
                }
                return flag1;
            }
            return flag1;
        } finally {
            if (httpconnection != null) {
                try {
                    httpconnection.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public final String b(String s) {
        return k.getAppProperty(s);
    }

    public final boolean c(String s) throws ConnectionNotFoundException {
        return k.platformRequest(s);
    }

    public final void run() {
        try {
            if (q == 3) {
                g();
            } else {
                if (q == 0)
                    h();
                return;
            }
        }
        catch (Throwable throwable) {
            ce.a("run", throwable);
        }
    }

    private void j() {
        String s = b("MIDlet-Version");
        q q1 = a.e("version");
        ce.b("Version jad: " + s + " rms: " + q1);
        if (m.k() || q1 == null || !s.equals(q1.toString())) {
            ce.b("Cleaning RMS");
            bl.a(new String[]{
                    "client", "R"
            });
            ce.b("Cleaned RMS");
            a = new bl("client");
            a.a("version", q.c(s));
        }
        d = s;
        ce.b("langpack: " + b("langpack"));
    }

    public final boolean a(Object obj) {
        return false;
    }

    public final void a(ba ba1, int i1) {
        if (i1 == -13)
            a.a("config", b.b());
    }

    public final String a(String s, String s1) {
        String s2;
        if ((s2 = b(s)) != null)
            return s2;
        q q1;
        if (c != null && !(q1 = c.b(s)).i())
            return q1.toString();
        if (s1 != null)
            return s1;
        else
            return "";
    }

    public static boolean e() {
        return d.a.a();
    }

    public final void a(String s, boolean flag) {
        if (s != null) {
            e = s;
            f = flag;
        }
    }

    public final void a(q q1) {
        b.a(q1);
        o = b.a("alert");
    }

    public final boolean f() {
        return o.g().d();
    }

    public final InputStream d(String s) {
        if (s == null)
            return null;
        byte abyte0[] = (byte[]) r.get(s);
        if (abyte0 != null)
            return new ByteArrayInputStream(abyte0);
        else
            return null;
    }

    public static void a(aq aq1) {
        aq1.i();
    }

    public static bt b(aq aq1) {
        return aq1.n;
    }

}